package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.GetUserInfoTask;

public class MyBalanceActivity extends BaseActivity {

    private Context mContext;
    private TextView balanceText;
    private Button rechargeBtn;
    private EatShowUserInfo userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mybalance);
        mContext = this;
        initView();
    }

    private void initView() {
//        userInfo = getEatShowApp().getmUserInfo();
        balanceText = (TextView) findViewById(R.id.balance);
//        if(userInfo != null){
//            balanceText.setText(userInfo.getBalance());
//        }
        rechargeBtn = (Button) findViewById(R.id.recharge_btn);
        rechargeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, RechargeActivity.class);
                startActivity(intent);
            }
        });
        ((TextView)findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getUserInfo();
    }

    private void getUserInfo(){
        GetUserInfoTask task = new GetUserInfoTask("");
        task.addListener(userInfoTaskListener);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener userInfoTaskListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(data != null){
                userInfo = (EatShowUserInfo) data;
                balanceText.setText(userInfo.getBalance());
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };
}
